﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AutoMapper;

using Happy.ExtentionMethods;

namespace Happy.AutoMapper
{
    /// <summary>
    /// 扩展了IMappingExpression{TSource, TDestination}。
    /// </summary>
    public static class MappingExpressionExtensions
    {
        /// <summary>
        /// 忽略所有非公共setter的属性的映射。
        /// </summary>
        public static IMappingExpression<TSource, TDestination> IgnoreNotPublicSetter<TSource, TDestination>(this IMappingExpression<TSource, TDestination> that)
        {
            that.MustNotNull("that");

            foreach (var property in typeof(TDestination).GetProperties())
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                if (!property.SetMethod.IsPublic)
                {
                    that.ForMember(property.Name, opt => opt.Ignore());
                }
            }

            return that;
        }
    }
}
